package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBCUtil1 {

    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/students1?useSSL=false&serverTimezone=UTC";
    private static String user = "root";
    private static String psw = "123456";

    int row = 0;
    static Connection con = null;

    public static Connection getCon(){

        try{
            // 1.加载数据库驱动
            Class.forName(driver);
            // 2.获取数据库连接
            con = DriverManager.getConnection(url, user, psw);

        } catch (Exception e){
            e.printStackTrace();
        }
        return con;
    }

    public static void close(ResultSet rs,Connection con,PreparedStatement pstm){
        // 关闭资源
        try{
            if (rs!=null){
                rs.close();
            }
            if (pstm!=null){
                pstm.close();
            }
            if (con!=null){
                con.close();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void close(Connection con,PreparedStatement pstm){
        // 关闭资源
        try{
            if (pstm!=null){
                pstm.close();
            }
            if (con!=null){
                con.close();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
